define(function(require, exports, module) {

	"use strict";
	var abpdata = require("../abp.js");
	var abp = abpdata.abpstart;
	var updateSell = {
		//销售订单编号
		"sellSerialNo": function() {
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.SELL
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").text(data.serialNo);
					if(data.success) {
						//console.log(data);
					} else {}
				})
				.fail(function(data) {

				});
		},
		//采购订单编号
		"buySerialNo": function() {
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.BUY
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").text(data.serialNo);
					if(data.success) {
						//console.log(data);
					} else {}
				})
				.fail(function(data) {

				});
		},
		"buyInSerialNo": function() {
			//采购入库单编号
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.BUY_INPUT
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					$("#code").text(data.serialNo);
				})
				.fail(function(data) {

				});
		},
		"sellOutSeriaNo": function() {
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.SELL_OUTPUT
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").text(data.serialNo);
				})
				.fail(function(data) {

				});
		},
		//更改订单页面
		"updateModel": function(option) {
			$(option.code).text(option.sellData.serialNo);
			$(option.createDate).text(option.sellData.serialDate.substring(0, 10));
			$(option.companyName).text(option.sellData.companyName);
			$(option.companyName).attr("data-id", option.sellData.companyId);
			$(option.busUserName).text(option.sellData.busUserName);
			$(option.busUserName).attr("data-id", option.sellData.busUserId);
			$(option.valorem).attr("data-id", option.sellData.taxRateType);
			$(option.valorem).text(option.sellData.taxRateTypeName);
			$(option.notes).val(option.sellData.notes);
			if(option.lastDate) {
				$(option.lastDate).text(option.sellData.deliveryDate.substring(0, 10));
			}
			if(option.goodsInfo) {
				//渲染后台数据页面
				var html = template(option.goodsInfo, option.sellData);
				$(option.goodsListShow).prepend(html);
				$(option.goodsListShow).show();
				$(option.lastInfo).show();
			}
			//总数量 总金额
			var allnumber, totalAmount;
			var arrays = [];
			var lastMoney = [];
			//遍历所有货品的数量
			$.each($(option.allNumber), function(i, v) {
				allnumber = Number($(v).text());
				arrays.push(allnumber);
			});
			//总数量
			//			console.log(lastMoney)
			for(var i = 0; i < arrays.length - 1; i++) {
				allnumber += arrays[i];
			}
			$(option.allNum).text(allnumber);
			//遍历所有货品的单金呢
			$.each($(option.allMoney), function(i, v) {
				totalAmount = Number($(v).attr("data-amount"));
				lastMoney.push(totalAmount);
			});
			//总金额
			for(var i = 0; i < lastMoney.length - 1; i++) {
				totalAmount += lastMoney[i];
			}
			$(option.allPrice).text(Number(totalAmount).toFixed(2));
		},
		//保存数据
		"commonData": function(sellDe) { //后台渲染数据赋值
			var goodsIndex = sessionStorage.getItem("goodsIndex");
			var goodsImg;
			$.each(sellDe, function(i, v) {
				if(i == goodsIndex) {
					if(sellDe[i].infoGoodsImg.length === 0) {
						goodsImg = "../../img/chioceProduct/product-icon.png";

					} else {
						goodsImg = sellDe[i].infoGoodsImg[0].imgPath;
					}
					$("#goodsSerial").text(sellDe[i].goodsSerial);
					$("#goodsName").text(sellDe[i].goodsName);
					$("#goodsName").attr("data-id", sellDe[i].goodsId);
					$(".product-img").attr("src", goodsImg);
					$("#whid").text((sellDe[i].whName || sellDe[i].outWhName));
					$("#whid").attr("data-id", (sellDe[i].whId || sellDe[i].outWhId));
					$("#whid").attr("data-unit", sellDe[i].unit);
					$("#whid").attr("data-unitNumber", sellDe[i].unitCode);
					$("#goodsNum").val(sellDe[i].unitCodeNumber);
					$("#discountMoney").val(Number(sellDe[i].discountAmount).toFixed(2));
					$("#lastMoney").text(Number(sellDe[i].amount).toFixed(2));
					$("#taxes").val(Number(sellDe[i].taxRate) * 100);
					$("#goodsPrice").val(Number(sellDe[i].price).toFixed(2));
					$("#notes").val(sellDe[i].notes);
					$("#discount").val(Number(sellDe[i].discount) * 100);
					$("#taxesMoney").text(Number(sellDe[i].baseAmountTax).toFixed(2));
					$("#noTaxesMoney").text(Number(sellDe[i].baseAmountNoIncludeTax).toFixed(2));
					$("#hasTaxesMoney").text(Number(sellDe[i].baseAmountIncludeTax).toFixed(2));
				}
				console.log($("#lastMoney").text());
			});
		}, //修改完成重新存储
		"commonSaveData": function(sellDe) { //后台渲染数据修改之后重新存储
			var goodsIndex = sessionStorage.getItem("goodsIndex");
			$.each(sellDe, function(i, v) {
				console.log(goodsIndex);
				if(i == goodsIndex) {
					sellDe[i].goodsSerial = $("#goodsSerial").text() || sellDe[i].goodsSerial;
					sellDe[i].goodsName = $("#goodsName").text() || sellDe[i].goodsName;
					sellDe[i].goodsId = $("#goodsName").attr("data-id") || sellDe[i].goodsId;
					sellDe[i].whName = $("#whid").text() || sellDe[i].whName;
					sellDe[i].whId = $("#whid").attr("data-id") || sellDe[i].whId;
					sellDe[i].unitCodeNumber = $("#goodsNum").val() || sellDe[i].unitCodeNumber;
					sellDe[i].discountAmount = $("#discountMoney").val() || sellDe[i].discountAmount;
					sellDe[i].amount = $("#lastMoney").text() || sellDe[i].amount;
					sellDe[i].taxRate = Number($("#taxes").val()) / 100 || sellDe[i].taxRate;
					sellDe[i].price = $("#goodsPrice").val() || sellDe[i].price;
					sellDe[i].notes = $("#notes").val() || sellDe[i].notes;
					sellDe[i].discount = Number($("#discount").val()) / 100 || sellDe[i].discount;
					sellDe[i].baseAmountTax = $("#taxesMoney").text() || sellDe[i].baseAmountTax;
					sellDe[i].baseAmountNoIncludeTax = $("#noTaxesMoney").text() || sellDe[i].baseAmountNoIncludeTax;
					sellDe[i].baseAmountIncludeTax = $("#hasTaxesMoney").text() || sellDe[i].baseAmountIncludeTax;
					sellDe[i].unit = $("#whid").attr("data-unit") || sellDe[i].unit;
					sellDe[i].unitCode = $("#whid").attr("data-unitNumber") || sellDe[i].unitCode;
				}
				
			});

		},
		"sellDeta": function() { //销售订单后台数据渲染
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("sellData"));
				var sellDe = sellData.sellDetail;
				updateSell.commonData(sellDe);
			}
		},
		"saveSellDeta": function() { //销售订单后台数据编辑再存储
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("sellData"));
				var sellDe = sellData.sellDetail;
				updateSell.commonSaveData(sellDe);
				sessionStorage.setItem("sellData", JSON.stringify(sellData));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		"buyDeta": function() { //采购订单后台数据渲染
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("sellData"));
				var sellDe = sellData.buyDetail;
				updateSell.commonData(sellDe);
			}
		},
		"saveBuyDeta": function() { //采购订单后台数据编辑在存储
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("sellData"));
				var sellDe = sellData.buyDetail;
				updateSell.commonSaveData(sellDe);
				sessionStorage.setItem("sellData", JSON.stringify(sellData));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		"buyInputDeta": function() { //采购入库订单后台数据渲染
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var inPurchaseData = JSON.parse(sessionStorage.getItem("inPurchaseData"));
				var sellDe = inPurchaseData.buyInputDetail;
				updateSell.commonData(sellDe);
			}
		},
		"saveBuyInputDeta": function() { //采购入库订单后台数据编辑在存储
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var inPurchaseData = JSON.parse(sessionStorage.getItem("inPurchaseData"));
				var sellDe = inPurchaseData.buyInputDetail;
				updateSell.commonSaveData(sellDe);
				sessionStorage.setItem("inPurchaseData", JSON.stringify(inPurchaseData));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		"allotStockDeta": function() { //库存调拨后台数据渲染
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("allotData"));
				var sellDe = sellData.whAllocateDetail;
				updateSell.commonData(sellDe);
			}
		},
		"saveAllotStockDeta": function() { //库存调拨后台数据编辑再存储
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("allotData"));
				var sellDe = sellData.whAllocateDetail;
				updateSell.commonSaveData(sellDe);
				sessionStorage.setItem("allotData", JSON.stringify(sellData));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		"outSalesDeta": function() { //销售出库记录后台数据渲染
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("salesData"));
				var sellDe = sellData.sellOutputDetail;
				updateSell.commonData(sellDe);
			}
		},
		"saveOutSalesDeta": function() { //销售出库记录后台数据编辑再存储
			var statusType = sessionStorage.getItem("statusType");
			if(statusType) {
				var sellData = JSON.parse(sessionStorage.getItem("salesData"));
				var sellDe = sellData.sellOutputDetail;
				updateSell.commonSaveData(sellDe);
				sessionStorage.setItem("salesData", JSON.stringify(sellData));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		"localDeta": function() { //本地数据渲染
			var goodsIndex = sessionStorage.getItem("goodsIndex");
			var localstatusType = sessionStorage.getItem("localstatusType");
			if(localstatusType) {
				var goodsInfoList = JSON.parse(sessionStorage.getItem("goodsInfoList"));
				var arrays = [];
				var objs = {};
				for(var i = 0; i < goodsInfoList.length; i++) {
					var goodsInfo = JSON.parse(goodsInfoList[i]);
					arrays.push(goodsInfo);
				}
				goodsInfoList = arrays;
				$.each(goodsInfoList, function(i, v) {
					if(i == goodsIndex) {
						try {
							var goodsImg = goodsInfoList[i].goodsImg || "../../img/chioceProduct/product-icon.png";
						} catch(e) {

						}
						$("#goodsSerial").text(goodsInfoList[i].goodsSerial);
						$("#goodsName").text(goodsInfoList[i].goodsName);
						$("#goodsName").attr("data-id", goodsInfoList[i].goodsId);
						$(".product-img").attr("src", goodsImg);
						$("#whid").text(goodsInfoList[i].goodsWhName);
						$("#whid").attr("data-id", goodsInfoList[i].goodsWhid);
						$("#goodsNum").val(goodsInfoList[i].goodsBuyNum);
						$("#discountMoney").val(Number(goodsInfoList[i].discountMoney).toFixed(2));
						$("#lastMoney").text(Number(goodsInfoList[i].lastMoney).toFixed(2));
						$("#taxes").val(Number(goodsInfoList[i].taxes) * 100);
						$("#goodsPrice").val(Number(goodsInfoList[i].goodsPrice).toFixed(2));
						$("#notes").val(goodsInfoList[i].notes);
						$("#discount").val(Number(goodsInfoList[i].discount) * 100);
						$("#taxesMoney").text(Number(goodsInfoList[i].taxesMoney).toFixed(2));
						$("#noTaxesMoney").text(Number(goodsInfoList[i].noTaxesMoney).toFixed(2));
						$("#hasTaxesMoney").text(Number(goodsInfoList[i].hasTaxesMoney).toFixed(2));
					}
				});
			}
		},
		"savelocalDeta": function() { //本地数据编辑在存储
			var goodsIndex = sessionStorage.getItem("goodsIndex");
			var localstatusType = sessionStorage.getItem("localstatusType");
			if(localstatusType) {

				var goodsInfoList = JSON.parse(sessionStorage.getItem("goodsInfoList"));
				var arrays = [];
				//				var objs = {};
				for(var i = 0; i < goodsInfoList.length; i++) {
					var goodsInfo = JSON.parse(goodsInfoList[i]);
					arrays.push(goodsInfo);
				}
				goodsInfoList = arrays;
				$.each(goodsInfoList, function(i, v) {
					if(i == goodsIndex) {
						goodsInfoList[i].goodsSerial = $("#goodsSerial").text();
						goodsInfoList[i].goodsName = $("#goodsName").text();
						goodsInfoList[i].goodsId = $("#goodsName").attr("data-id");
						goodsInfoList[i].goodsWhName = $("#whid").text();
						goodsInfoList[i].goodsWhid = $("#whid").attr("data-id");
						goodsInfoList[i].goodsBuyNum = $("#goodsNum").val();
						goodsInfoList[i].discountMoney = $("#discountMoney").val();
						goodsInfoList[i].lastMoney = $("#lastMoney").text();
						goodsInfoList[i].allMoney = Number($("#goodsNum").val()) * Number($("#goodsPrice").val());
						goodsInfoList[i].taxes = Number($("#taxes").val()) / 100;
						goodsInfoList[i].goodsPrice = $("#goodsPrice").val();
						goodsInfoList[i].notes = $("#notes").val();
						goodsInfoList[i].discount = Number($("#discount").val()) / 100;
						goodsInfoList[i].taxesMoney = $("#taxesMoney").text();
						goodsInfoList[i].noTaxesMoney = $("#noTaxesMoney").text();
						goodsInfoList[i].hasTaxesMoney = $("#hasTaxesMoney").text();
					}
				});
				var arrays = [];
				//				var objs = {};
				for(var i = 0; i < goodsInfoList.length; i++) {
					var goodsInfo = JSON.stringify(goodsInfoList[i]);
					arrays.push(goodsInfo);
				}
				sessionStorage.setItem("goodsInfoList", JSON.stringify(arrays));
				sessionStorage.removeItem("libraryName");
				sessionStorage.removeItem("libraryNameId");
				mui.back();
			}
		},
		//删除本地存储数据货品明细
		"deleteGoodsDetail": function(option, callback) {

			mui("body").on("tap", option.delete, function() {
				//遍历li标签添加index属性
				$.each($(option.goodsIn), function(i, v) {
					$(v).attr("data-index", i);
				});
				//获取index
				var index = Number($(this).parent().data("index"));

				var btnArray = ['否', '是'];
				mui.confirm('确认删除', '提  示', btnArray, function(e) {
					if(e.index == 1) {
						//根据index删除与之对应数据
						for(var i = 0; i < option.array.length; i++) {
							if(index == i) {
								option.array.splice(index, 1);
							}
						}
						sessionStorage.setItem(option.goodsInfoList, JSON.stringify(option.array));
						$(option.goodsIn).eq(index).remove();

						//重新计算数量与金额
						//						getFunList.addGoodsInfoList("goodsList","#goodsListShow");
						updateSell.allnumAndAllMoney();
						if(callback && callback !== undefined) {
							callback();
						}
					} else {

					}
				});
			});
		},
		//			updateArray,data
		//删除后台本地存储数据货品明细
		"deleteUpdateGoodsDetail": function(option, callback, callback1) {
			mui("body").on("tap", option.update, function() {
				//遍历li标签添加index属性
				$.each($(option.goodsInList), function(i, v) {
					$(v).attr("data-index", i);
				});
				//获取index
				var index = Number($(this).parent().data("index"));
				var btnArray = ['否', '是'];
				mui.confirm('确认删除', '提  示', btnArray, function(e) {
					if(e.index == 1) {
						//根据index删除与之对应数据
						for(var i = 0; i < option.updateArr.length; i++) {
							if(index == i) {
								option.updateArr.splice(index, 1);
							}
						}
						sessionStorage.setItem(option.sellData, JSON.stringify(option.data));
						$(option.goodsInList).eq(index).remove();
						//重新计算数量与金额
						updateSell.allnumAndAllMoney();
						if(callback && callback !== undefined) {
							callback();
						}
					} else {

					}
				});
			});
		},
		"allnumAndAllMoney": function() {
			//总数量 总金额
			var allnumber, totalAmount;
			var arrays = [];
			var lastMoney = [];
			//遍历所有货品的数量
			$.each($("#goodsListShow .goodsNum"), function(i, v) {
				allnumber = Number($(v).text());
				arrays.push(allnumber);
			});
			//总数量
			//			console.log(lastMoney)
			for(var i = 0; i < arrays.length - 1; i++) {
				allnumber += arrays[i];
			}
			//		console.log(allnumber)
			$("#allNum").text(allnumber);
			//遍历所有货品的单金呢
			$.each($("#goodsListShow .product-money"), function(i, v) {
				totalAmount = Number($(v).attr("data-amount"));
				lastMoney.push(totalAmount);
			});
			//总金额
			for(var i = 0; i < lastMoney.length - 1; i++) {
				totalAmount += lastMoney[i];
				
			}
			console.log(totalAmount)
			$("#allPrice").text(Number(totalAmount).toFixed(2));
				
		}

	};
	module.exports = updateSell;

});